﻿using System;
using System.Linq;
using System.Windows;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Doc;
using Spire.Doc.Documents;
using System.IO;
using iText.Kernel.Pdf.Canvas.Parser;
using System.Diagnostics;

namespace PdfConvert
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        protected string _path = "";
        public MainWindow()
        {
            InitializeComponent(); 
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string path = string.Empty;

            Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();

            // 过滤文件扩展名，设置接受的扩展名
            openFileDlg.DefaultExt = ".pdf";
            openFileDlg.Filter = "可执行程序 (.pdf)|*.pdf";

            // 设置初始目录
            openFileDlg.InitialDirectory = this._path;

            //允许多选
            //openFileDlg.Multiselect = true;
            //openFileDlg.Filter = "All files (*.*)|*.*";

            // 调用ShowDialog方法显示对话框
            Nullable<bool> result = openFileDlg.ShowDialog();
            // 显示名称
            if (result == true)
            {
                path = openFileDlg.FileName;
                txtFilePath.Text = path;
            }
        }

        private void btnPdfConvert_Click(object sender, RoutedEventArgs e)
        {
            string strValidityDate = "2026-06-06";
            btnPdfConvert.IsEnabled = false;
            if (txtName.Text.Trim()!="丁学宝" || txtPhoneNumber.Text.Trim() != "13402589995")
            {
                 MessageBox.Show("开发者信息不正确！");
            }
            else 
            {
                if (DateTime.Now > DateTime.Parse(strValidityDate))
                {
                    MessageBox.Show("文件已过期，请联系【丁学宝】获取最新程序！");
                }
                else
                {
                    if (string.IsNullOrEmpty(txtFilePath.Text.Trim()))
                    {
                        MessageBox.Show("请选择文件！");
                    }
                    else
                    {
                        PdfDocument document = new PdfDocument();
                        document.LoadFromFile(txtFilePath.Text);
                        string strPath = Path.GetDirectoryName(txtFilePath.Text.Trim());
                        string strName = Path.GetFileNameWithoutExtension(txtFilePath.Text.Trim());
                        document.SaveToFile(strPath + "/" + strName + ".docx", Spire.Pdf.FileFormat.DOCX);

                        MessageBox.Show("转换成功，新文件在源文件同目录中，若加密pdf，只能转换前10页！");
                        btnPdfConvert.IsEnabled = true;
                    }
                }
            }
            
        }

        /// <summary>
        /// iTextSharp库PDF文件转Word文件（去除文档格式，，没有页码限制）
        /// </summary>
        /// <param name="pdfFilePath"></param>
        /// <param name="wordFilePath"></param>
        //public void ConvertPdfToWordByText(string pdfFilePath, string wordFilePath)
        //{

        //    using (iText.Kernel.Pdf.PdfReader reader = new iText.Kernel.Pdf.PdfReader(pdfFilePath))
        //    {
        //        using (iText.Kernel.Pdf.PdfDocument pdfDoc = new iText.Kernel.Pdf.PdfDocument(reader))
        //        {
        //            Spire.Doc.Document doc = new Spire.Doc.Document();
        //            for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
        //            {
        //                iText.Kernel.Pdf.PdfPage page = pdfDoc.GetPage(i);
        //                var strategy = new iText.Kernel.Pdf.Canvas.Parser.Listener.LocationTextExtractionStrategy();
        //                PdfCanvasProcessor parser = new PdfCanvasProcessor(strategy);
        //                parser.ProcessPageContent(page);
        //                string textFromPage = strategy.GetResultantText();
        //                var paragraph = doc.AddSection().AddParagraph();
        //                paragraph.AppendText(textFromPage);
        //            }

        //            doc.SaveToFile(wordFilePath, Spire.Doc.FileFormat.Docx);
        //        }
        //    }
        //}

        protected void test1()
        {
            string outputWordFilePath = string.Empty;
            outputWordFilePath = "C:\\Users\\Administrator\\Desktop\\";

            // PDF文件路径
            string pdfFilePath = "C:\\Users\\Administrator\\Desktop\\FHHREX24FATP131 Inv & PL.pdf";
            // Word文档输出路径
            string wordFilePath = "C:\\Users\\Administrator\\Desktop\\output.docx";

            // 加载PDF文档
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(pdfFilePath);

            // 创建一个新的Word文档
            Document word = new Document();

            for (int i = 0; i < pdf.Pages.Count; i++)
            {
                PdfPageBase page = pdf.Pages[i];

                // 创建Word中的段落
                Section section = word.AddSection();
                Paragraph paragraph = section.AddParagraph();

                // 将PDF页面的文本内容转换为Word段落文本
                //for (int j = 0; j < page.TextSelections.Count; j++)
                //{
                //    string text = page.TextSelections[j].Text;
                //    paragraph.AppendText(text);
                //}

                //// 如果PDF页面包含图片，将其转换为Word中的图片
                //if (page.Images.Count > 0)
                //{
                //    foreach (PdfImage image in page.Images)
                //    {
                //        ImageSection imgSection = section.AddImage(image.Data);
                //        paragraph = imgSection.AddParagraph();
                //    }
                //}
            }

            // 保存Word文档
            word.SaveToFile(wordFilePath, Spire.Doc.FileFormat.Docx);
        }
    }
}
